﻿using System.Collections.Specialized;
using System.Configuration;
using System.Net.Configuration;
using System.Web;
using System.Web.Configuration;
using IApplicationSettingsProvider = VideoSeller.Core.Interfaces.IApplicationSettingsProvider;

namespace VideoSeller.Business.Providers
{
	public class ApplicationSettingsProvider : IApplicationSettingsProvider
	{
		public string VideoSellerConnectionString
		{
			get
			{
				var setting = ConfigFileConnections["VideoSellerConnectionString"];
				return setting.ConnectionString;
			}
		}

		public SmtpSection SmtpMailSettings
		{
			get
			{
				var config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
				var mailSettings = (MailSettingsSectionGroup) config.GetSectionGroup("system.net/mailSettings");

				if (mailSettings == null || mailSettings.Smtp == null || mailSettings.Smtp.Network.Host == null)
				{
					throw new ConfigurationErrorsException("Smtp configuration required in web.config system.net/mailSettings/smtp section.");
				}

				return mailSettings.Smtp;
			}
		}

		private static NameValueCollection ConfigFileSettings
		{
			get { return WebConfigurationManager.AppSettings; }
		}

		private static ConnectionStringSettingsCollection ConfigFileConnections
		{
			get { return WebConfigurationManager.ConnectionStrings; }
		}
	}
}